package com.wang.bootbuilder.aop;

import com.wang.bootbuilder.tools.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.wang.bootbuilder.annotation.LogAsp;

import java.lang.reflect.Method;

@Aspect
@Slf4j
@Component
public class TestAop {

//    @Pointcut(value = "execution(* com.wang.bootbuilder.controller.RoleController.getRolesList(..))")
//    public void pointDemo() {
//
//    }
//
//    @Before(value = "execution(* com.wang.bootbuilder.controller.RoleController.getRolesList(..))")
//    public void before() {
//        log.info("我在controller前面被执行了");
//    }
//
//    @After(value = "pointDemo()")
//    public void after() {
//        log.info("...after");
//    }
//
//    @AfterReturning(value = "pointDemo()")
//    public void afterReturn() {
//        log.info(".....afterReturn");
//    }
//
//    @AfterThrowing(value = "execution(* com.wang.bootbuilder.controller.RoleController.getRolesList(..))")
//    public void afterThrowing() {
//        log.info(".....afterThrowing");
//    }
//
//    @Around(value = "pointDemo()")
//    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        log.info("...around之前");
//        proceedingJoinPoint.proceed();
//        log.info("...around之后");
//    }

//    @After("@annotation(com.wang.bootbuilder.annotation.LogAsp)")
//    public void after() {
//        log.info("我是后置通知");
//    }
//
//    @Before("@annotation(com.wang.bootbuilder.annotation.LogAsp)")
//    public void before() {
//        log.info("我是前置通知");
//    }


    @Around("@annotation(com.wang.bootbuilder.annotation.LogAsp)")
    public JsonResult cutPoint(ProceedingJoinPoint joinPoint)  {
        // 获取代理对象
        Object target = joinPoint.getTarget();

        // 获取被代理对象
        Object aThis = joinPoint.getThis();

        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        LogAsp annotation = method.getAnnotation(LogAsp.class);
        String value = annotation.value();
        log.info(value + "开始执行了...");
        JsonResult proceed = null;
        try {
            proceed = (JsonResult)joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        log.info(value + "开始执行完了...");
        return proceed;
    }

}
